package com.pubinfo.passbook.data.controller.sync;

import com.pubinfo.passbook.common.entity.TSourceInvoice;
import com.pubinfo.passbook.common.mapper.TSourceInvoiceMapper;
import com.pubinfo.passbook.common.mapper.TSubsidiaryPrivateKeyMapper;
import com.pubinfo.passbook.common.model.bo.SubsidiaryInfo;
import com.pubinfo.passbook.common.model.vo.InvoiceList;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.service.master.TInvoiceService;
import com.pubinfo.passbook.common.service.request.tax.RequestTaxServer;
import com.pubinfo.passbook.data.service.InvoiceFilePackageInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "debug/tax")
@Api(tags = "同步税务数据管理")
@Slf4j
public class SyncTaxController {
  @Autowired
  RequestTaxServer taxServer;
  @Autowired
  InvoiceFilePackageInfoService invoiceFilePackageInfoService;
  @Resource
  TInvoiceService invoiceService;
  @Resource
  TSourceInvoiceMapper invoiceMapper;
  @Resource
  TSubsidiaryPrivateKeyMapper keyMapper;
  
  @PostMapping("batchSyncByDate")
  @ApiOperation(value = "指定时间内进行同步操作  时间格式 yyyy-MM-dd")
  public JsonResult<Integer> batchSyncByDate(String startDate, String endDate) {
    return JsonResult.success(taxServer.sync(startDate, endDate));
  }
  
  @PostMapping("syncByDate")
  @ApiOperation(value = "指定一天进行同步操作")
  public JsonResult<Integer> syncByDate(String startDate) {
    
    return JsonResult.success(taxServer.saveByDate(startDate));
    
  }

  @PostMapping("batchSyncInvoiceFileByDate")
  @ApiOperation(value = "指定时间内进行版式文件下载  时间格式 yyyy-MM-dd")
  public JsonResult<Integer> batchSyncInvoiceFileByDate(String startDate, String endDate) {
    return JsonResult.success(invoiceFilePackageInfoService.sync(startDate, endDate));
  }

  @PostMapping("syncInvoiceFileByDate")
  @ApiOperation(value = "指定一天进行版式文件下载")
  public JsonResult<Integer> syncInvoiceFileByDate(String startDate) {

    return JsonResult.success(invoiceFilePackageInfoService.saveByDateAndTaskId(startDate,null));

  }


//
//  @PostMapping("queryByTime")
//  @ApiOperation(value = "直接调用查询总数接口")
//  public JsonResult<QueryByTimeResult> queryByTime(@RequestBody JSONObject startDate) {
//    return JsonResult.success(taxServer.queryByTime(startDate));
//  }
  
  @PostMapping("queryByDateAndInfo")
  @ApiOperation(value = "根据日期和公司查询发票")
  public JsonResult<List<TSourceInvoice>> queryByDateAndInfo(String info, String date) {
    return JsonResult.success(taxServer.queryByDateAndInfo(info, date));
    
  }
  
  @GetMapping("selectRepetition")
  @ApiOperation(value = "查询重复数据")
  public JsonResult<List<InvoiceList>> selectRepetition() {
    return JsonResult.success(invoiceMapper.selectRepetition());
  }
  
  @DeleteMapping("delete")
  @ApiOperation(value = "删除指定日志记录id下所有发票数据")
  public JsonResult<Integer> delete(String logId) {
    
    return JsonResult.success(invoiceService.deleteByLogId(logId));
  }
  
  @GetMapping("listAllSubsidiaryInfo")
  @ApiOperation(value = "查询所有企业主体信息")
  public JsonResult<List<SubsidiaryInfo>> listAllSubsidiaryInfo() {
    return JsonResult.success(keyMapper.listTaxAllSubsidiaryInfo(null));
  }
}
